package com.project.frba.bifront.client.view.dashboard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.PanelListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.form.event.CheckboxListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.DashboardServices;
import com.project.frba.bifront.client.rpc.DashboardServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class CreationDashboardWindow extends Composite{
	
	private Window mainWindow = new Window();
	private DashboardMainPanel parent;
	private TextBox txDashboardName = new TextBox();
	private Radio rdDashboardType2 = new Radio("2 Graficos", "radio");
	private Radio rdDashboardType4 = new Radio("4 Graficos", "radio");
	private Integer selectedType = Dashboard.DUAL;
	private Button btAccept = new Button("Aceptar");
	private Button btCancel = new Button("Cancelar");
	
	public CreationDashboardWindow(DashboardMainPanel panel){
		this.parent = panel;
		mainWindow.setLayout(new FitLayout());
		mainWindow.setModal(true);
		mainWindow.setWidth(400);
		mainWindow.setHeight(160);
		mainWindow.setTitle("Nuevo tablero de control");
		Panel mainPanel = new Panel();
		
		
		Panel radioPanel = new Panel();
		rdDashboardType2.setValue(true);
		radioPanel.add(rdDashboardType2);
		radioPanel.add(rdDashboardType4);
		
		
		txDashboardName.setWidth("220");
		
		//Labels
		Label lblName = new Label("Nombre");
		Label lblType = new Label("Cantidad de Graficos");
		
		FlexTable tableLayout = new FlexTable();
		tableLayout.setWidget(0,0, lblName);
		tableLayout.setWidget(0, 1, txDashboardName);
		tableLayout.setWidget(1,0, lblType);
		tableLayout.setWidget(1, 1, radioPanel);
		loadActions();
		
		mainPanel.add(tableLayout);
		
		mainWindow.addListener(new PanelListenerAdapter(){
			public void onClose(Panel panel) {
				parent.getDashboards();
				parent.refreshDashboard();
			}
		});
		
		mainWindow.setButtons(new Button[]{btAccept, btCancel});
		mainWindow.add(mainPanel);
		initWidget(mainWindow);
		mainWindow.show();
	}

	private void loadActions() {
		btAccept.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				if(!txDashboardName.getText().equals("")){
					DashboardServicesAsync dashboardServices = (DashboardServicesAsync)GWT.create(DashboardServices.class);	
					Dashboard dashboard = new Dashboard(selectedType);
					dashboard.setName(txDashboardName.getText());
					
					AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
						public void onFailure(Throwable caught) {
							new MessageWindow("Error al crear un nuevo tablero de control.");
						}
						public void onSuccess(Boolean result) {
							new MessageWindow("Su tablero de control se ha creado correctamente.");
						}
					};
					
					dashboardServices.createDashboard(dashboard, callback);
					parent.getDashboards();
					mainWindow.close();
				} else {
					new MessageWindow("Debe ingresar un nombre para el tablero de comando.");
					
				}
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
		rdDashboardType2.addListener(new CheckboxListenerAdapter(){
			@Override
			public void onCheck(Checkbox field, boolean checked) {
				selectedType = Dashboard.DUAL;
			}
		});
		
		rdDashboardType4.addListener(new CheckboxListenerAdapter(){
			@Override
			public void onCheck(Checkbox field, boolean checked) {
				selectedType = Dashboard.QUAD;
			}
		});		
		
		
	}

	public void show() {
		mainWindow.show();
	}

}
